package com.jsb.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.jsb.service.UserService;
import com.jsb.service.WishService;
import com.jsb.service.WishbuyService;
import com.jsb.common.utils.StringUtils;
import com.jsb.model.User;
import com.jsb.model.Wish;
import com.jsb.model.Wishbuy;
import com.jsb.model.WishbuyExample;
import com.jsb.model.WishbuyExample.Criteria;

@Controller
public class WishbuyController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/wishbuy/list.do";
	
	@Resource
	private WishbuyService wishbuyService;
	@Resource
	private WishService wishService;
	@Resource
	private UserService userService;
	
	@RequestMapping("/wishbuy/list")
	public String list(
			Model model,String phone,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		WishbuyExample wishbuyExample = new WishbuyExample();
		List<User> userByLikePhone = userService.getUserByLikePhone(phone);
		for (User user : userByLikePhone) {
			String userid = user.getPhoneNumber();
			Criteria createCriteria = wishbuyExample.createCriteria();
			if(StringUtils.isNotEmpty(userid)){
				createCriteria.andUserIdLike("%"+userid+"%");
			}
			wishbuyExample.or(createCriteria);
		}
		int totalCount = wishbuyService.countWishbuy(wishbuyExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		wishbuyExample.setPageIndex((pageNum - 1) * pageSize);
		wishbuyExample.setPageSize(pageSize);
		wishbuyExample.setOrderByClause("createDate DESC");
		List<Wishbuy> wishbuys = wishbuyService.getPageWishbuy(wishbuyExample);
		for (Wishbuy wishbuy : wishbuys) {
			String id = wishbuy.getWishId();
			Wish wishById = wishService.getWishById(id);
			if(null == wishById){
				continue;
			}
			wishbuy.setWishId(wishById.getName());
		}
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("wishbuys", wishbuys);
		model.addAttribute("phone", phone);
		return "/Wishbuy/list";
	}
	
	@RequestMapping("/wishbuy/toAdd")
	public String toAdd(Model model) {
		return "/Wishbuy/add";
	}

	@RequestMapping("/wishbuy/add")
	public String add(Model model, @ModelAttribute("wishbuy") Wishbuy wishbuy) {
		wishbuyService.addWishbuy(wishbuy);
		return redirect;
	}

	@RequestMapping("/wishbuy/del")
	public String del(Model model,
			@RequestParam(value = "id", required = true) String id) {
		wishbuyService.delWishbuyById(id);
		return redirect;
	}

	@RequestMapping("/wishbuy/toEdit")
	public String toEdit(Model model,
			@RequestParam(value = "id", required = true) String id) {
		Wishbuy wishbuy = wishbuyService.getWishbuyById(id);
		model.addAttribute("wishbuy", wishbuy);
		return "/Wishbuy/edit";
	}

	@RequestMapping("/wishbuy/edit")
	public String edit(Model model, @ModelAttribute("wishbuy") Wishbuy wishbuy) {
		wishbuyService.updateWishbuy(wishbuy);
		return redirect;
	}

	@RequestMapping("/wishbuy/view")
	public String view(Model model,
			@RequestParam(value = "id", required = true) String id) {
		Wishbuy wishbuy = wishbuyService.getWishbuyById(id);
		model.addAttribute("wishbuy", wishbuy);
		return "/Wishbuy/view";
	}
}
